<cfcomponent>
	<cfproperty name="pk"/>
	<cfproperty name="object"/>
	<cfproperty name="dbtype"/>	
	<cfproperty name="dsn"/>	
	<cfproperty name="appname"/>	
	<cfproperty name="packagename"/>	
	
	<cfset this.pk				= "" />
	<cfset this.object 			= "" />
	<cfset this.dbtype			= "" />
	<cfset this.dsn				= "" />
	<cfset this.appname			= "" />
	<cfset this.packagename		= "" />

	<cffunction name="init" returntype="any">
		<cfargument name="object"		default=""/>
		<cfargument name="pk"			default=""/>
		<cfargument name="dbtype"		default=""/>
		<cfargument name="dsn"			default=""/>
		<cfargument name="appname"		default=""/>
		<cfargument name="packagename"	default=""/>
		<cfset this.object 		= arguments.object />
		<cfset this.pk			= arguments.pk />
		<cfset this.dbtype		= arguments.dbtype />
		<cfset this.dsn			= arguments.dsn />
		<cfset this.appname		= arguments.appname />
		<cfset this.packagename	= arguments.packagename />
		<cfreturn this />
	</cffunction>
	
	
	<cffunction name="changestoapply">
		<cfset servicefilelocation = expandpath('../../service/#this.object#Service.cfc') />
		<cfif not fileexists( servicefilelocation )>
			<cfreturn "service file"/>
		</cfif>
		<cffile action="read" file="#expandpath('../../xml/transfer.xml')#" variable="transferfilecontents"/>
		<cfif not find("object name=""#this.object#""",transferfilecontents)>
			<cfreturn "transferdef"/>
		</cfif>
		<cfreturn "" />
	</cffunction>
	
	<cffunction name="applychanges">
		<cfscript>
			addservice();
			addtransferdef();
		</cfscript>
	</cffunction>

	<cffunction name="checkservicefile">
		<cfset servicefilelocation = expandpath('../../service/#this.object#Service.cfc') />
		<cfif not fileexists( servicefilelocation )>
			<cffile action="copy" source="#expandpath('../templates/webservice/webservice.cfc')#" destination="#servicefilelocation#" />
			<cfset replacetokensinfile( servicefilelocation )/>
		</cfif>
	</cffunction>
	
	<cffunction name="addservice">
		<cfset checkservicefile() />
		<cfset servicefilelocation = expandpath('../../service/#this.object#Service.cfc') />
	</cffunction>
	
	<cffunction name="addtransferdef">
		<cffile action="read" file="#expandpath('../../xml/transfer.xml')#" variable="transferfilecontents"/>
		<cfif not find("object name=""#this.object#""",transferfilecontents)>
			<cfset scriptname 	= replace(getdirectoryfrompath(cgi.SCRIPT_NAME), "/", ".", "ALL") />
			<cfset scriptname 	= mid(scriptname,2, len(scriptname))/>
			<cfset table 		= createobject("component", "#scriptname#.datasource.#this.dbtype#").init( this.dsn ) />
			<cfset table.setTable( ucase(this.object) ) />
			<cfset table.setcomponentpath( this.object ) />
			<cfset thexml 		= table.getTableXML() /> 
			<cffile action="read" file="#expandpath('../templates/webservice/transfer.xsl')#" variable="xslDoc">
			<cfset transformedXML 		= XmlTransform(thexml, xslDoc)>
			<cfoutput>
			<cfsavecontent variable="newcontent">	#trim(transformedXML)#
				</package></cfsavecontent>
			</cfoutput>
			<cfset transferfilecontents = replace(transferfilecontents, "</package>", "#newcontent#") />
			<cffile action="write" file="#expandpath('../../xml/transfer.xml')#" output="#replacetokensinstring(transferfilecontents)#"/>
		</cfif>
	</cffunction>	
	
	<cffunction name="replacetokensinfile">
		<cfargument name="file"/>
		<cffile action="read" file="#arguments.file#" variable="filecontents"/>
		<cfset filecontents = replace(filecontents, "%objectname%", "#this.object#", "ALL") />
		<cfset filecontents = replace(filecontents, "%primarykey%", "#this.pk#", "ALL") />
		<cfset filecontents = replace(filecontents, "%packagename%", "#this.packagename#", "ALL") />
		<cfset filecontents = replace(filecontents, "%appname%", "#this.appname#", "ALL") />
		<cfset filecontents = replace(filecontents, "&lt;", "<", "ALL") />
		<cfset filecontents = replace(filecontents, "&gt;", ">", "ALL") />
		<cffile action="write" file="#arguments.file#" output="#filecontents#"/>
	</cffunction>

	<cffunction name="replacetokensinstring">
		<cfargument name="text"/>
		<cfset arguments.text = replace(arguments.text, "%objectname%", "#this.object#", "ALL") />
		<cfset arguments.text = replace(arguments.text, "%primarykey%", "#this.pk#", "ALL") />
		<cfset arguments.text = replace(arguments.text, "&lt;", "<", "ALL") />
		<cfset arguments.text = replace(arguments.text, "&gt;", ">", "ALL") />
		<cfreturn arguments.text />
	</cffunction>
		
</cfcomponent>